<?php
namespace app\common\model;

use think\Db;

class Info extends Base
{
    protected $pk = 'id';

    //定义模型关联的表名
    protected $table = 'infos';

    //自定义初始化
    protected static function init()
    {

    }

    public function getCreateTimeLabelAttr($value, $data)
    {
        if (empty($data['create_time'])) {
            return '未知';
        }
        return date('Y-m-d H:i:s', $data['create_time']);
    }

    //城市省份
    public function getProvinceLabelAttr($value, $data)
    {
        $parents = [];
        $option_value = OptionValue::where('value', $data['province'])->where('parent_id', '<>', 0)->find();
        if (!empty($option_value)) {
            $parents = $this->getParents(OptionValue::select()->toArray(), $option_value, 'id', 'parent_id', true);
            $parents = array_reverse($parents);
            $parents = array_column($parents, 'name');
        }
        $parents[] = $option_value['name'];
        if (empty($parents)) {
            return '';
        }
        $result = implode('', $parents);
        return $result;
    }

    public function getCityNameAttr($value, $data)
    {
        $option_value = OptionValue::where('value', $data['province'])->where('parent_id', '<>', 0)->find();
        if (empty($option_value)) {
            return '';
        }

        return $option_value['name'];
    }

    public function getCityCodeAttr($value, $data)
    {
        $option_value = OptionValue::where('value', $data['province'])->where('parent_id', '<>', 0)->find();
        if (empty($option_value)) {
            return '';
        }

        return $option_value['value'];
    }

    public function getProvinceNameAttr($value, $data)
    {
        $parents = [];
        $option_value = OptionValue::where('value', $data['province'])->where('parent_id', '<>', 0)->find();
        if (!empty($option_value)) {
            $parents = $this->getParents(OptionValue::select()->toArray(), $option_value, 'id', 'parent_id', true);
            $parents = array_reverse($parents);
            $parents = array_column($parents, 'name');
        }
        $parents[] = $option_value['name'];

        return current($parents);
    }

    public function getProvinceCodeAttr($value, $data)
    {
        $parents = [];
        $option_value = OptionValue::where('value', $data['province'])->where('parent_id', '<>', 0)->find();
        if (!empty($option_value)) {
            $parents = $this->getParents(OptionValue::select()->toArray(), $option_value, 'id', 'parent_id', true);
            $parents = array_reverse($parents);
            $parents = array_column($parents, 'value');
        }
        $parents[] = $option_value['value'];

        return current($parents);
    }
    
     //城市省份
    public function getProvinceIdsAttr($value, $data)
    {
        $parents = [];
        $option_value = OptionValue::where('value', $data['province'])->where('parent_id', '<>', 0)->find();
        if (!empty($option_value)) {
            $parents = $this->getParents(OptionValue::select()->toArray(), $option_value, 'id', 'parent_id', true);
            $parents = array_reverse($parents);
            $parents = array_column($parents, 'value');
        }
        $parents[] = $option_value['value'];
       
      
        return $parents;
    }

    public function getLoanLabelAttr($value, $data)
    {
        $field = 'loan';
        if (isset($data[$field.'_label']) && (!empty($data[$field.'_label']))) {
            return $data[$field.'_label'];
        }

        $option_value = Db::name('option_values')->alias('ov')->field('ov.name')->join('option_items o', 'o.id=ov.item_id', 'left')->where('ov.value', $data[$field])->where('ov.is_show', $this->is_show)->where('o.ename', $field)->find();
        if (empty($option_value)) {

            return '';
        }

        return $option_value['name'];
    }
    
    public function getEducationLabelAttr($value, $data)
    {
        $field = 'education';
        if (isset($data[$field.'_label']) && (!empty($data[$field.'_label']))) {
            return $data[$field.'_label'];
        }
        $option_value = Db::name('option_values')->alias('ov')->field('ov.name')->join('option_items o', 'o.id=ov.item_id', 'left')->where('ov.value', $data[$field])->where('ov.is_show', $this->is_show)->where('o.ename', $field)->find();
        if (empty($option_value)) {

            return '';
        }

        return $option_value['name'];
    }

    public function getLoanTermLabelAttr($value, $data)
    {
        $field = 'loan_term';
        if (isset($data[$field.'_label']) && (!empty($data[$field.'_label']))) {
            return $data[$field.'_label'];
        }
        $option_value = Db::name('option_values')->alias('ov')->field('ov.name')->join('option_items o', 'o.id=ov.item_id', 'left')->where('ov.value', $data[$field])->where('ov.is_show', $this->is_show)->where('o.ename', $field)->find();
        if (empty($option_value)) {

            return '';
        }

        return $option_value['name'];

    }

    public function getLoanUseLabelAttr($value, $data)
    {
        $field = 'loan_use';
        if (isset($data[$field.'_label']) && (!empty($data[$field.'_label']))) {
            return $data[$field.'_label'];
        }
      
        $option_value = Db::name('option_values')->alias('ov')->field('ov.name')->join('option_items o', 'o.id=ov.item_id', 'left')->where('ov.value', $data[$field])->where('ov.is_show', $this->is_show)->where('o.ename', $field)->find();
        if (empty($option_value)) {

            return '';
        }

        return $option_value['name'];

    }

    public function getProfessionalLabelAttr($value, $data)
    {
        $field = 'professional';
        if (isset($data[$field.'_label']) && (!empty($data[$field.'_label']))) {
            return $data[$field.'_label'];
        }
        $option_value = Db::name('option_values')->alias('ov')->field('ov.name')->join('option_items o', 'o.id=ov.item_id', 'left')->where('ov.value', $data[$field])->where('ov.is_show', $this->is_show)->where('o.ename', $field)->find();
        if (empty($option_value)) {

            return '';
        }

        return $option_value['name'];

    }


    public function getJdbaitiaoLabelAttr($value, $data)
    {
        $field = 'jdbaitiao';
        if (isset($data[$field.'_label']) && (!empty($data[$field.'_label']))) {
            return $data[$field.'_label'];
        }
        $option_value = Db::name('option_values')->alias('ov')->field('ov.name')->join('option_items o', 'o.id=ov.item_id', 'left')->where('ov.value', $data[$field])->where('ov.is_show', $this->is_show)->where('o.ename', $field)->find();
        if (empty($option_value)) {

            return '';
        }

        return $option_value['name'];

    }

    public function getHasGongjjLabelAttr($value, $data)
    {
        $field = 'has_gongjj';
        if (isset($data[$field.'_label']) && (!empty($data[$field.'_label']))) {
            return $data[$field.'_label'];
        }
        $option_value = Db::name('option_values')->alias('ov')->field('ov.name')->join('option_items o', 'o.id=ov.item_id', 'left')->where('ov.value', $data[$field])->where('ov.is_show', $this->is_show)->where('o.ename', $field)->find();
        if (empty($option_value)) {

            return '';
        }

        return $option_value['name'];

    }

    public function getCreditHistoryLabelAttr($value, $data)
    {
        $field = 'credit_history';
        if (isset($data[$field.'_label']) && (!empty($data[$field.'_label']))) {
            return $data[$field.'_label'];
        }
        $option_value = Db::name('option_values')->alias('ov')->field('ov.name')->join('option_items o', 'o.id=ov.item_id', 'left')->where('ov.value', $data[$field])->where('ov.is_show', $this->is_show)->where('o.ename', $field)->find();
        if (empty($option_value)) {

            return '';
        }

        return $option_value['name'];

    }

    public function getZhimaCreditLabelAttr($value, $data)
    {
        $field = 'zhima_credit';
        if (isset($data[$field.'_label']) && (!empty($data[$field.'_label']))) {
            return $data[$field.'_label'];
        }
        $option_value = Db::name('option_values')->alias('ov')->field('ov.name')->join('option_items o', 'o.id=ov.item_id', 'left')->where('ov.value', $data[$field])->where('ov.is_show', $this->is_show)->where('o.ename', $field)->find();
        if (empty($option_value)) {

            return '';
        }

        return $option_value['name'];

    }

    public function getHasFundLabelAttr($value, $data)
    {
        $field = 'has_fund';
        if (isset($data[$field.'_label']) && (!empty($data[$field.'_label']))) {
            return $data[$field.'_label'];
        }
        $option_value = Db::name('option_values')->alias('ov')->field('ov.name')->join('option_items o', 'o.id=ov.item_id', 'left')->where('ov.value', $data[$field])->where('ov.is_show', $this->is_show)->where('o.ename', $field)->find();
        if (empty($option_value)) {

            return '';
        }

        return $option_value['name'];

    }

    public function getMicroLoanLabelAttr($value, $data)
    {
        $field = 'micro_loan';
        if (isset($data[$field.'_label']) && (!empty($data[$field.'_label']))) {
            return $data[$field.'_label'];
        }
        $option_value = Db::name('option_values')->alias('ov')->field('ov.name')->join('option_items o', 'o.id=ov.item_id', 'left')->where('ov.value', $data[$field])->where('ov.is_show', $this->is_show)->where('o.ename', $field)->find();
        if (empty($option_value)) {

            return '';
        }

        return $option_value['name'];

    }

    public function getLifeInsuranceLabelAttr($value, $data)
    {
        $field = 'life_insurance';
        if (isset($data[$field.'_label']) && (!empty($data[$field.'_label']))) {
            return $data[$field.'_label'];
        }
        $option_value = Db::name('option_values')->alias('ov')->field('ov.name')->join('option_items o', 'o.id=ov.item_id', 'left')->where('ov.value', $data[$field])->where('ov.is_show', $this->is_show)->where('o.ename', $field)->find();
        if (empty($option_value)) {

            return '';
        }

        return $option_value['name'];

    }

    public function getHousePropertyLabelAttr($value, $data)
    {
        $field = 'house_property';
        if (isset($data[$field.'_label']) && (!empty($data[$field.'_label']))) {
            return $data[$field.'_label'];
        }
        $option_value = Db::name('option_values')->alias('ov')->field('ov.name')->join('option_items o', 'o.id=ov.item_id', 'left')->where('ov.value', $data[$field])->where('ov.is_show', $this->is_show)->where('o.ename', $field)->find();
        if (empty($option_value)) {

            return '';
        }

        return $option_value['name'];

    }

    public function getCarPropertyLabelAttr($value, $data)
    {
        $field = 'car_property';
        if (isset($data[$field.'_label']) && (!empty($data[$field.'_label']))) {
            return $data[$field.'_label'];
        }
        $option_value = Db::name('option_values')->alias('ov')->field('ov.name')->join('option_items o', 'o.id=ov.item_id', 'left')->where('ov.value', $data[$field])->where('ov.is_show', $this->is_show)->where('o.ename', $field)->find();
        if (empty($option_value)) {

            return '';
        }

        return $option_value['name'];

    }

    public function getIdCardHideAttr($value, $data)
    {
        if (!isset($data['id_card']) && empty($data['id_card'])) {
            return str_repeat('*', 18);
        }

        $result = substr($data['id_card'], 0, 4) . str_repeat('*', strlen($data['id_card']) - 8) . substr($data['id_card'], -4);

        return $result;

    }
    
    
    public function getSourceStatusArrAttr($value, $data)
    {
        switch ($data['source_status']) {
            case 1:
                $result['source_status_label'] = '已获取';
                $result['source_status_class'] = 'layui-btn-default';
                break;
            case -1:
                $result['source_status_label'] = '已退回';
                $result['source_status_class'] = 'layui-btn-danger';
                break;
            default:
                $result['source_status_label'] = '未获取';
                $result['source_status_class'] = 'layui-btn-primary';
                break;
        }

        return $result;
    }
    
    
    public function getChannelIdLabelAttr($value, $data)
    {
        $channel = Db::name('channels')->where('id', '=', $data['channel_id'])->find();
        
        if (empty($channel)) {
            
            return '';
        }
        
        
        
        return $channel['name'];
    }

public function getMonthlyIncomeLabelAttr($value, $data)
    {
        $field = 'monthly_income';
        if (isset($data[$field.'_label']) && (!empty($data[$field.'_label']))) {
            return $data[$field.'_label'];
        }
        $option_value = Db::name('option_values')->alias('ov')->field('ov.name')->join('option_items o', 'o.id=ov.item_id', 'left')->where('ov.value', $data[$field])->where('ov.is_show', $this->is_show)->where('o.ename', $field)->find();
        if (empty($option_value)) {

            return '';
        }

        return $option_value['name'];

    }

}